package com.zhou.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.entity.Driver;
import com.zhou.mapper.DriverMapper;
import com.zhou.service.DriverService;
import com.zhou.vo.driver.InsertDriverVO;
import com.zhou.vo.driver.UpdateDriverVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DriverServiceImpl implements DriverService {
    @Resource
    private DriverMapper driverMapper;

    /**
     * 新增司机个人信息
     * @param vo
     * @return
     */
    @Override
    public String add(InsertDriverVO vo) {
        if (StringUtils.isEmpty(vo)){
            throw new RuntimeException("参数不能为空，请重试");
        }
        //将vo里的数据赋值到实体类
        Driver driver=new Driver();
        BeanUtils.copyProperties(vo,driver);
        driver.setDriverId(UUID.fastUUID().toString());
        int insert = driverMapper.insert(driver);
        if (insert==0){
            throw new RuntimeException("新增失败，请重试");
        }
        return driver.getDriverId();
    }

    /**
     * 修改司机信息
     * @param vo
     * @return
     */
    @Override
    public String update(UpdateDriverVO vo) {
        if (StringUtils.isEmpty(vo)){
            throw new RuntimeException("参数不能为空，请重试");
        }
        //将vo装换成实体类
        Driver driver=new Driver();
        BeanUtils.copyProperties(vo,driver);
        int i = driverMapper.updateById(driver);
        if (i==0){
            throw new RuntimeException("更新失败，请重试");
        }
        return driver.getDriverId();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Driver getById(String id) {
        if (StringUtils.isEmpty(id)){
            throw new RuntimeException("参数不能为空，请重试");
        }
        Driver driver = driverMapper.selectById(id);
        if (StringUtils.isEmpty(driver)){
            throw new RuntimeException("没有查到数据，请重试");
        }
        return driver;
    }

    /**
     * 分页查询
     * @param page 1
     * @param pageSize 10
     * @return 所有司机的信息
     */
    @Override
    public List<Driver> getAll(Integer page, Integer pageSize) {
        IPage<Driver> pg=new Page<>(page,pageSize);
        LambdaQueryWrapper<Driver> wrapper=new LambdaQueryWrapper<>();
        List<Driver> records = driverMapper.selectPage(pg, wrapper).getRecords();
        if (CollectionUtils.isEmpty(records)||records.size()==0){
            throw new RuntimeException("没有查到数据，请重试");
        }
        return records;
    }
}
